package com.liohi.Interceptor;


//import com.liohi.mapper.PermissionMapper;
import com.liohi.mapper.RoleMenuMapper;
import com.liohi.mapper.UserRoleMapper;
import com.liohi.ThreadPool.AppRuntimeEnv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class JwtInterceptor implements HandlerInterceptor {
//    @Autowired
//    private user_roleMapper user_roleMapper;
//    @Autowired
//    private role_permissionMapper role_permissionMapper;
//    @Autowired
//    private PermissionMapper permissionMapper;
//
//    @Override
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws IOException {
////        try {
////            String token = httpServletRequest.getParameter("token");
////            //无token,默认游客访问模式
////            if (token == null) {
////                User curUser = new User("游客");
////                //id待修改
////                List<Integer> roleList = user_roleMapper.roleIdList(5);
////                HashSet<String> permission = new HashSet<>();
////                for (int roleId : roleList) {
////                    //角色对应权限
////                    List<Integer> permissionIdList = role_permissionMapper.permissionIdList(roleId);
////                    for (int permissionId : permissionIdList) {
////                        //权限对应的资源路径
////                        String permissions = permissionMapper.permissions(permissionId);
////                        permission.add(permissions);
////                    }
////                }
//////                设置游客权限
////                AppRuntimeEnv.getContext().setPermission(permission);
////                AppRuntimeEnv.getContext().setUser(curUser);
////                return true;
////            }
////            //有token且未过期,ThreadLocal保存当前登录用户信息
////            if (!JwtUtils.checkToken(token)) {
////                throw new Exception(MyErrorCode.TOKEN_IS_NULL.getCode() + MyErrorCode.TOKEN_IS_NULL.getMsg());
////            }
////            Claims claims = JwtUtils.resvoleToken(token);
////            User currentUser = new User();
////            currentUser.setId((int) claims.get("id"));
////            currentUser.setName((String) claims.get("name"));
////            AppRuntimeEnv.getContext().setUser(currentUser);
////            AppRuntimeEnv.getContext().setPermission(new HashSet<>((ArrayList<String>) claims.get("permission")));
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//        return true;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        AppRuntimeEnv.clearContext();
//    }


}